from .Keyword import Keyword
from django.db import models
from control.models.User import User
import uuid
from .Institution import Institution
from .Department import Department

# 作者信息
class Author(models.Model):
    uuid = models.UUIDField(primary_key = True,auto_created = True,default = uuid.uuid4,editable = False)
    name = models.CharField(verbose_name="名字", max_length=256)		                        
    zh_name = models.CharField(verbose_name="中文名", max_length=128, null=True, blank=True)	
    orcid = models.CharField(verbose_name="orcid", max_length=32, null=True, blank=True)	
    email = models.EmailField(verbose_name="邮箱", null=True, blank=True)				
    address = models.CharField(verbose_name="详细地址", max_length=256, null=True, blank=True)
    country = models.CharField(verbose_name="所在国家", max_length=64, null=True, blank=True)
    city = models.CharField(verbose_name="所在城市", max_length=64, null=True, blank=True)		
    phone = models.CharField(verbose_name="电话号码", max_length=32, null=True, blank=True)		
    education_degree = models.TextField(verbose_name="学历或职称信息", null=True, blank=True)	
    research_field = models.TextField(verbose_name="研究领域描述", null=True, blank=True)	
    cited_count = models.IntegerField(verbose_name="文章被引用数", default=0)						
    homepage = models.URLField(verbose_name="个人主页链接", max_length=256, null=True, blank=True)
    description = models.TextField(verbose_name="作者简介", null=True, blank=True)                          
    is_reviewing = models.BooleanField(verbose_name="审核状态", default=False)

    tag = models.ManyToManyField(Keyword, verbose_name="标签")
    institutions = models.ManyToManyField(Institution, verbose_name="所属机构")	# 一个作者可以属于多个机构
    departments = models.ManyToManyField(Department, verbose_name="所属部门")		# 每个部门与机构对应
                               
    creator = models.ForeignKey(User, verbose_name="创建者", related_name="author_creator", on_delete=models.CASCADE)				
    create_date = models.DateTimeField(verbose_name='创建时间', auto_now_add=True)
    last_editor = models.ForeignKey(User, verbose_name="最后编辑者", related_name="author_last_editor", on_delete=models.CASCADE)	
    last_edit_date = models.DateTimeField(verbose_name='最后编辑时间', auto_now=True)					
    reviewer = models.ForeignKey(User, verbose_name="审核者", related_name="author_reviewer", on_delete=models.CASCADE, null=True)
    review_date = models.DateTimeField(null=True, blank=True, verbose_name='审核时间')
    
    class Meta:
        ordering = ['-name', '-zh_name', '-orcid', '-country', '-cited_count']
        verbose_name = "Author"           # 单数名称
        verbose_name_plural = "Authors"   # 复数名称
        indexes = [
            models.Index(fields=['name', 'zh_name'], name='final_query_idx_author')
        ]

    def __str__(self):
        return self.name